package jesss.commands;

import com.amazonaws.AmazonClientException;
import com.amazonaws.services.s3.model.Bucket;
import jesss.options.JesssOptions;

import java.util.List;

/**
 * User: Lee
 * Date: 8/16/13
 * Time: 11:35 AM
 */
public class ListBucketsCommand<T> extends AbstractJesssCommand {

    JesssOptions jesssOptions = null;

    public ListBucketsCommand(JesssOptions jesssOptions) {
        this.jesssOptions = jesssOptions;
    }

    @Override
    public CommandResult<List<Bucket>> executeCommand() {
        CommandResult<List<Bucket>> commandResult = new CommandResult<>();
        List<Bucket> buckets;

        try {
            buckets = jesssOptions.getAmazonS3().listBuckets();
            for (Bucket bucket : buckets) {
                System.out.printf("%s\t%s\t%s\t%s%n", bucket.getName(), bucket.getCreationDate(), bucket.getOwner().getDisplayName(), bucket.getOwner().getId());
            }
            commandResult.setResultValue(buckets);
            commandResult.setResultStatus(ResultStatus.Success);
        } catch (AmazonClientException e) {
            commandResult.setResultStatus(ResultStatus.Error);
            commandResult.setStatusMessage("Unable to get list of buckets.");
            addExceptionInfoToCommandResult(commandResult, e);
        }

        return commandResult;
    }
}
